package com.bac.bacplatform;

import java.lang.reflect.Type;
import java.util.ArrayList;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.bac.javabeans.GasolinePlan;
import com.bac.utils.Config;
import com.bac.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 惠加油首页,套餐介绍
 *  
 * @author lerry
 * 
 */
public class BuyCouponActivity extends BaseActivity implements OnClickListener {

	private ArrayList<GasolinePlan> mPlans;

	private ListView mListView;

	private CouponAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coupon);

		mListView = (ListView) findViewById(R.id.list_plans);
		getCouponPlans();
	}

	@Override
	public void init() {

		findViewById(R.id.ly_coupon_info).setOnClickListener(this);
		findViewById(R.id.ly_coupon_agreen).setOnClickListener(this);

		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (mPlans.size() == 0) {
					return;
				}
				Intent intentToBuy = new Intent(BuyCouponActivity.this,
						PreferentialGasActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable(GasolinePlan.GASOLINEPLAN_KEY,
						mPlans.get(position));
				intentToBuy.putExtras(bundle);
				startActivityIn(intentToBuy);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ly_coupon_info:
			Intent intentToDetail = new Intent(this,
					BacAgreementWebActivity.class);
			intentToDetail.putExtra("urltype", "detail");
			startActivityIn(intentToDetail);
			break;

		case R.id.ly_coupon_agreen:
			Intent intentToAgree = new Intent(this,
					BacAgreementWebActivity.class);
			intentToAgree.putExtra("urltype", "agree");
			startActivityIn(intentToAgree);
			break;
		default:
			break;
		}

	}

	public void getCouponPlans() {
		/**
		 * 成功返回结果
		 */
		Listener<String> sL = new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgress();
				Log.i("TAG----success", response.toString());

				Type listType = new TypeToken<ArrayList<GasolinePlan>>() {
				}.getType();
				Gson gson = new Gson();
				mPlans = gson.fromJson(response, listType);

				mAdapter = new CouponAdapter(getApplicationContext(), mPlans);

				mListView.setAdapter(mAdapter);

				Log.i("BuyCouponActivity-----", mPlans.get(0).getName());
				Log.i("BuyCouponActivity-----", mPlans.get(1).getName());
			}
		};

		showLoadingProgress();

		String path = Config.URL_COUPON_PLAN;
		Util.httpRequestToGet(path, sL, this);
	}

	class CouponAdapter extends BaseAdapter {

		private ArrayList<GasolinePlan> gasolinePlans;
		private Context mContext = null;

		public CouponAdapter(Context context, ArrayList<GasolinePlan> plans) {
			this.mContext = context;
			this.gasolinePlans = plans;

		}

		@Override
		public int getCount() {
			return gasolinePlans.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			GasolinePlan gasolinePlan = gasolinePlans.get(position);
			final ViewHolder viewHolder;

			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.coupon_item, null);

				viewHolder = new ViewHolder();

				viewHolder.planTv = (TextView) convertView
						.findViewById(R.id.tv_coupon_item);

				viewHolder.divider = (ImageView) convertView
						.findViewById(R.id.img_divider);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.planTv.setText(gasolinePlan.getName());
			if (position == gasolinePlans.size() - 1) {
				viewHolder.divider.setVisibility(View.GONE);
			} else {
				viewHolder.divider.setVisibility(View.VISIBLE);
			}

			return convertView;
		}

		class ViewHolder {
			TextView planTv;
			ImageView divider;
		}
	}
}
